package org.springblade.modules.repair.vo.plan;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springblade.common.cache.DictCache;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.repair.entity.MaintainPlanTemplate;
import org.springblade.modules.repair.entity.MaintainTemplate;
import org.springblade.modules.repair.enums.MaintainPlanContentEnum;
import org.springblade.modules.repair.enums.TaskStatusEnum;
import org.springblade.modules.repair.utils.DateUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description 首页-我的任务分页-已完成列表
 * @Author ysq
 * @Date 2024/1/11
 */
@Data
public class PlanFinishOrderHomePageV4VO {

	@ApiModelProperty(value = "项目id")
	private Long buildingId;

	@ApiModelProperty(value = "项目名称")
	private String buildingName;

	@ApiModelProperty(value = "项目纬度")
	private BigDecimal latitude;

	@ApiModelProperty(value = "项目经度")
	private BigDecimal longitude;

	@ApiModelProperty(value = "项目别名，多个逗号隔开")
	private String aliasStr;

	@ApiModelProperty(value = "项目地址")
	private String buildingAddress;

	@ApiModelProperty("距离文案")
	private String kmDesc;

	@ApiModelProperty("工单列表")
	List<PlanWorkItemList> list;

	@Data
	public static class PlanWorkItemList {
		@ApiModelProperty(value = "工单id")
		private Long planId;

		@ApiModelProperty(value = "电梯id")
		private Long elevatorId;

		@ApiModelProperty(value = "工单类型 1: 保养, 2: 维修, 3: 困人, 4: 配件, 5: 事务, 6: 质检")
		private Integer typeStatus;

		@ApiModelProperty(value = "工单类型名称")
		private String typeStatusName;

		@JsonFormat(pattern = "yyyy-MM-dd")
		@ApiModelProperty(value = "开始时间")
		private Date planDate;

		@ApiModelProperty(value = "结束时间")
		private Date finishDate;

		@ApiModelProperty(value = "0: 未延迟, 1: 延迟")
		private Integer isDeferred;

		@ApiModelProperty(value = "状态")
		private Integer status;

		@ApiModelProperty(value = "状态名称")
		private String statusName;

		@ApiModelProperty(value = "工单更新时间")
		private Date updateTime;

		@ApiModelProperty(value = "工单创建时间")
		private Date createTime;

		private String workUserInfos;

		@ApiModelProperty(value = "工单人员信息")
		private LinkedList<PlanOrderHomeUserVO> signUsers;

		@ApiModelProperty(value = "设备代码")
		private String privateNum;

		@ApiModelProperty(value = "品牌名称")
		private String brandName;

		@ApiModelProperty(value = "电梯位置")
		private String elevatorAddress;

		@ApiModelProperty("紧急程度 1:非常紧急 2:紧急 3:普通 4:较低")
		private Integer emerProcType;

		/**
		 * 保养详情
		 */
		@ApiModelProperty("保养名称")
		private String planCheckName;

		@ApiModelProperty("停梯状态： 1-正常 2-停梯")
		private Integer elevatorIsStop;

		@ApiModelProperty("逾期文案")
		private String overdueName;

		@ApiModelProperty("1:非年审，2:年审")
		private Integer auditType;

		@ApiModelProperty("电梯年审状态1:非年审，0:年审")
		private Integer elevatorAuditType;

		@ApiModelProperty("超时文案")
		private String timeliness;

		@ApiModelProperty("保养模板")
		List<MaintainTemplate> maintainTemplates;

		/**
		 * 维修详情
		 */
		@ApiModelProperty("维修故障原因")
		private List<String>  problemItemNames;
		private String problemItem;

		private String malPartsNames;

		@ApiModelProperty("维修层站id")
		private String malFloorStandIds;

		@ApiModelProperty("维修层站名称")
		private String malFloorStandNames;

		/**
		 * 救援详情
		 */
		@ApiModelProperty(value = "报警类型 0-困人，1-伤人")
		private Integer trapType;

		@ApiModelProperty(value = "报警备注")
		private String trapDesc;

		@ApiModelProperty(value = "服务器时间")
		@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
		private Date serviceDate;

		/**
		 * 配件详情
		 */
		@ApiModelProperty(value = "部件名称集合")
		private String partsName;

		@ApiModelProperty("层站id")
		private String floorStandIds;

		@ApiModelProperty("层站名称")
		private String floorStandNames;

		@ApiModelProperty(value = "部件数量")
		private Integer partsNum;

		/**
		 * 事务详情
		 */
		@ApiModelProperty("事务工单的描述信息")
		private String affairDesc;

		/**
		 * 质检详情
		 */
		@ApiModelProperty(value = "1:保养质检，2:重复故障质检，3:维保移交质检，4:安装移交质检，5:常规质检")
		private Integer checkType;

		@ApiModelProperty(value = "逾期时间")
		private Date overdueDate;

		@ApiModelProperty(value = "签字时间")
		private Date maintainSignatureTime;


		@ApiModelProperty("北京时间")
		private String beijingDate = DateUtil.format(DateUtils.beijingDate(), "yyyy-MM-dd HH:mm:ss");

		private Integer overdueType;

		private String overdueTypeName;

		/**
		 * 计划截止时间
		 */
		private Date planFinishDate;

		/**
		 * 转派截止时间
		 */
		private Date redeployFinishDate;

		/**
		 * 转派逾期状态 1-未逾期 2-即将逾期 3-未逾期
		 */
		private Integer redeployOverdueStatus;

		/**
		 * 重新打开次数
		 */
		private Integer reopenCount;


		public List<PlanOrderHomeUserVO> getSignUsers() {
			if (Func.isBlank(this.workUserInfos)) {
				return signUsers;
			}

			this.signUsers = new LinkedList<>();
			String[] userArr = Func.split(this.workUserInfos, ",");
			for (String user : userArr) {
				PlanOrderHomeUserVO planOrderHomeUser = new PlanOrderHomeUserVO();
				String[] userInfoArr = Func.split(user, "|");
				planOrderHomeUser.setUserId(Long.valueOf(userInfoArr[0]));
				planOrderHomeUser.setUserName(userInfoArr[1]);
				planOrderHomeUser.setIsPlanStatus(Boolean.valueOf(userInfoArr[2]));
				planOrderHomeUser.setIsTakePartIn(Boolean.valueOf(userInfoArr[3]));

				planOrderHomeUser.setUserWxArriveStatus(Convert.toInt(userInfoArr[4], -1));
				planOrderHomeUser.setPlanArriveDate(Convert.toStr(userInfoArr[5], "1970-01-01 00:00:00"));
				planOrderHomeUser.setCreateTime(Convert.toStr(userInfoArr[6], "1970-01-01 00:00:00"));
				planOrderHomeUser.setSignDate(Convert.toStr(userInfoArr[7], "1970-01-01 00:00:00"));
				this.signUsers.add(planOrderHomeUser);
			}
			return signUsers;
		}

		public String getStatusName() {
			return TaskStatusEnum.getTaskName(this.typeStatus, this.status);
		}

		public String getTypeStatusName() {
			return MaintainPlanContentEnum.getName(this.typeStatus);
		}

		public void setPartsName(String partsName) {
			if(Func.isNotEmpty(partsName)){
				this.partsName = partsName.replace("&amp;&amp;",">");
			}
		}

		public void setFloorStandNames(String floorStandNames) {
			if (Func.isNotEmpty(floorStandNames)) {
				this.floorStandNames = floorStandNames.replace("&amp;&amp;", ">");
			}
		}

		public void setMalPartsNames(String malPartsNames) {
			if (Func.isNotEmpty(malPartsNames)) {
				this.malPartsNames = malPartsNames.replace("&amp;&amp;", ">");
			}
		}

		public void setMalFloorStandNames(String malFloorStandNames) {
			if (Func.isNotEmpty(malFloorStandNames)) {
				this.malFloorStandNames = malFloorStandNames.replace("&amp;&amp;", ">");
			}
		}

		public void setProblemItem(String problemItem) {
			if (Func.isNotBlank(problemItem)) {
				List<String> problemKeys = Func.toStrList(problemItem);
				List<String> problemNames = new ArrayList<>();
				for (String problemKey : problemKeys) {
					String problemName = DictCache.getValue("fault_problem", problemKey);
					problemNames.add(problemName);
				}
				this.problemItemNames = problemNames;
			}
			this.problemItem = problemItem;
		}

		public void setPlanCheckName(String planCheckName) {
			if (Func.isNotEmpty(planCheckName) && planCheckName.contains("%")) {
				List<String> strings = Func.toStrList(planCheckName);
				List<MaintainPlanTemplate> list = new ArrayList<>(10);
				for (String str : strings) {
					String[] split = str.split("%");
					MaintainPlanTemplate maintainPlanTemplate = new MaintainPlanTemplate();
					maintainPlanTemplate.setTemplateName(split[1]);
					maintainPlanTemplate.setSort(Convert.toInt(split[2], 0));
					list.add(maintainPlanTemplate);
				}
				List<String> collect = list.stream().sorted(Comparator.comparing(MaintainPlanTemplate::getSort)).map(MaintainPlanTemplate::getTemplateName).collect(Collectors.toList());
				this.planCheckName = String.join(",", collect);
			} else {
				this.planCheckName = planCheckName;
			}
		}
	}

}
